(function(window,undefined){
	var Sungmin = {};
	window.Sungmin = window.Sungmin || Sungmin;
	
	/*page-set*/
	var reportPage = Sungmin.reportPage = {};
	var sellerCommon = Sungmin.sellerCommon = {};
	
	sellerCommon.sellerNav = function(){
		var sellerNav = $('.sellerNav'); /*상점 탬 셀렉터*/
		var ownerStoreId = sessionStorage.getItem('owner_store_id');
		sellerNav.find('.menu1 > a').attr('href','seller_store.php?id='+ownerStoreId);
	};
	
	/*google_chart load*/
	/*단골추이*/
	// reportPage.favoriteFlowBlock = function(){
		// google.load("visualization", "1", {packages : ["corechart"]});
		// google.setOnLoadCallback(drawChart);
		// function drawChart() {
			// var data = google.visualization.arrayToDataTable([
		
				// ['주간', '전체고객', '신규고객', '핵심고객', '이탈고객'], 
				// ['03/01', 123, 23, 21, 7], 
				// ['03/08', 132, 33, 26, 2], 
				// ['03/15', 125, 46, 32, 8], 
				// ['03/22', 146, 56, 48, 2], 
				// ['03/29', 245, 23, 21, 7], 
				// ['04/05', 256, 33, 26, 2], 
				// ['04/12', 213, 46, 32, 8]
			// ]);
// 
			// var options = { };
// 
			// var chart = new google.visualization.LineChart(document.getElementById('chart_customer'));
			// chart.draw(data, options);
		// }
	// };
	
	/*model > 시간대별 방문 고객수*/
	var loadVisitorCount = function(successCallback){
		var arrResult = []; /*초기화*/
		var store_id = sessionStorage.getItem('owner_user_id');
		
		$.ajax({
			type:'post',
			url:ajaxURL+'report.php?action=loadVisitorCount',
			dataType:'json',
			async:false,
			data:{store_id:store_id},
			success:function(result){
				var arrTemp0 = [0,0,0,0,0,0,0,0];
				var arrTemp1 = [0,0,0,0,0,0,0,0];
				var arrTemp2 = [0,0,0,0,0,0,0,0];
				
				$.each(result[0],function(){
					var hour = (this.created_date.split(' ')[1].split(':')[0]/3).toFixed(0);
					
					arrTemp0[hour] = arrTemp0[hour] + 1;
				});
				
				$.each(result[1],function(){
					var hour = (this.created_date.split(' ')[1].split(':')[0]/3).toFixed(0);
					
					arrTemp1[hour] = arrTemp1[hour] + 1;
				});
				
				$.each(result[2],function(){
					var hour = (this.created_date.split(' ')[1].split(':')[0]/3).toFixed(0);
					
					arrTemp2[hour] = arrTemp2[hour] + 1;
				});
				
				arrResult.push(arrTemp0);
				arrResult.push(arrTemp1);
				arrResult.push(arrTemp2);
				
				successCallback(arrResult);
			}
		});
	};
	
	/*시간대별 방문고객 수*/
	reportPage.visitorBlock = function(){
		loadVisitorCount(function(result){
			google.load("visualization", "1", {packages : ["corechart"]});
			google.setOnLoadCallback(drawChart);
			// google.setOnLoadCallback(drawChart);
			function drawChart() {
				var data = google.visualization.arrayToDataTable([
					['Time', '3월13일', '3월 14일', '3월 15일'],
					['0~3시',result[2][0],result[1][0],result[0][0]],
					['3~6시',result[2][1],result[1][1],result[0][1]],
					['6~9시',result[2][2],result[1][2],result[0][2]],
					['9~12시',result[2][3],result[1][3],result[0][3]],
					['12~15시',result[2][4],result[1][4],result[0][4]],
					['15~18시', result[2][5],result[1][5],result[0][5]], 
					['18~21시', result[2][6],result[1][6],result[0][6]],
					['21~24시', result[2][7],result[1][7],result[0][7]]
				]);
				var options = {};
				var chart = new google.visualization.ColumnChart(document.getElementById('chart_customer_visit_1'));
				chart.draw(data, options);
			}
		});
	};
	
	/*model > 단골연령대*/
	var loadStoreCustomerAges = function(successCallback){
		var arrResult = [0,0,0,0,0,0,0];
		var store_id = sessionStorage.getItem('owner_user_id');
		
		$.ajax({
			type:'post',
			url:ajaxURL+'report.php?action=loadStoreCustomerAges',
			dataType:'json',
			async:false,
			data:{store_id:store_id},
			success:function(result){		
				$.each(result,function(){
					var customerYear = this.birthday.split('-')[0];
					var nowYear = new Date().getFullYear();
					var ages = Math.floor((nowYear - customerYear + 1)/10);
					
					if(ages < arrResult.length){
						arrResult[ages] = arrResult[ages] + 1;
					}
				});
				
				successCallback(arrResult);
			}
		});
	};
	/*단골연령대*/
	reportPage.favoriteAgesBlock = function(){
		loadStoreCustomerAges(function(result){
			google.load("visualization", "1", {packages : ["corechart"]});
			google.setOnLoadCallback(drawChart);
			function drawChart() {
				var data = new google.visualization.DataTable();
				data.addColumn('string', 'Topping');
				data.addColumn('number', 'Slices');
				data.addRows([
					['10대', result[1]], 
					['20대', result[2]], 
					['30대', result[3]], 
					['40대', result[4]], 
					['50대', result[5]], 
					['60대', result[6]]
				]);
		
				var options = { };
				var chart = new google.visualization.PieChart(document.getElementById('chart_customer_age'));
				chart.draw(data, options);
			}
		});
	};
	
	/*단골구성*/
	// reportPage.favoriteTypeBlock = function(){
		// google.setOnLoadCallback(drawChart);
		// function drawChart() {
			// var data = new google.visualization.DataTable();
			// data.addColumn('string', 'Topping');
			// data.addColumn('number', 'Slices');
			// data.addRows([['핵심고객', 12], ['일반고객', 32], ['신규고객', 36]]);
// 
			// var options = { };
			// var chart = new google.visualization.PieChart(document.getElementById('chart_customer_type'));
			// chart.draw(data, options);
		// }
	// };
	
	var loadStoreGender = function(successCallback){
		var arrResult = [0,0];/*0 : male , 1 : female*/
		var store_id = sessionStorage.getItem('owner_user_id');
		
		$.ajax({
			type:'post',
			url:ajaxURL+'report.php?action=loadStoreGender',
			dataType:'json',
			async:false,
			data:{store_id:store_id},
			success:function(result){
				$.each(result,function(){
					if(this.gender == 'male'){
						arrResult[0] = arrResult[0] + 1;
					} else if(this.gender == 'femail'){
						arrResult[1] = arrResult[1] + 1;
					}
				});
				
				successCallback(arrResult);
			}
		});
	};
	/*단골남녀비율*/
	reportPage.favoriteGenderBlock = function(){
		loadStoreGender(function(result){
			google.load("visualization", "1", {packages : ["corechart"]});
			google.setOnLoadCallback(drawChart);
			function drawChart() {
				var data = new google.visualization.DataTable();
				data.addColumn('string', 'Topping');
				data.addColumn('number', 'Slices');
				data.addRows([
					['남성', result[0]],
					['여성', result[1]]
				]);
	
				var options = { };
				var chart = new google.visualization.PieChart(document.getElementById('chart_customer_gender'));
				chart.draw(data, options);
			}
		});
	};
	
	
})(window);